#include "mainwindow.h"
#include "ui_mainwindow.h"

void MainWindow::on_actionNewGame_triggered()
{
    if (!(QMessageBox::information(this, tr("开始新游戏？"), tr("确定要开始一个新游戏吗？"), tr("是"), tr("否")))) {
        this->block_init();
        this->set_score();
    }
}

void MainWindow::on_pushButton_clicked()
{
    this->on_actionReset_triggered();
}

void MainWindow::on_actionReset_triggered()
{
    if (!(QMessageBox::information(this, tr("确定重置？"), tr("确定要重置方块吗？"), tr("是"), tr("否")))) {
        int preScore = this->score;
        this->block_init();
        this->set_score(preScore);
    }
}

void MainWindow::on_actionAbout_triggered()
{
    QMessageBox::about(this, "关于", "小辣稽敬献 : )");
    // 给某人的彩蛋
    // if ((QMessageBox::information(this, tr("关于"), tr("小辣稽敬献 : )"), tr("确定"), tr("取消")))) {
    //     if (!(QMessageBox::information(this, tr("嗯？"), tr("谁是世界上最可爱的家伙？"), tr("丁丁"), tr("小辣稽")))) {
    //         QMessageBox::about(this, "答对了！", "答对了！丁丁最可爱！");
    //     } else {
    //         QMessageBox::about(this, "嘤嘤嘤", "害羞.jpg");
    //     }
    // }
}

void MainWindow::on_actionRecord_triggered()
{
    // TODO: 读取游戏记录
}

void MainWindow::on_actionQuit_triggered()
{
    if (!(QMessageBox::information(this, tr("确定退出？"), tr("确定要退出吗？\n进度不会被保存"), tr("是"), tr("否")))) {
        QApplication* app;
        app->quit();
    }
}

void MainWindow::btn_init()
{
    for (int i = 0; i < COL_SIZE; i++) {
        for (int j = 0; j < ROW_SIZE; j++) {
            DragPushButton* p = new DragPushButton();
            p->setParent(this);
            p->setIJ(i, j);
            p->setGeometry(
                GAME_FIELD_X + BLOCK_EDGE * j + BLOCK_MARGIN,
                GAME_FIELD_Y + BLOCK_EDGE * i + BLOCK_MARGIN,
                BLOCK_EDGE - BLOCK_MARGIN * 2,
                BLOCK_EDGE - BLOCK_MARGIN * 2);
            p->setFlat(true);
            p->setAutoFillBackground(true);

            connect(p, SIGNAL(swapBlockSig(int, int, int, int)),
                this, SLOT(swapBlock(int, int, int, int)));

            btns[i][j] = p;
        }
    }
}

void MainWindow::block_init()
{
    for (int i = 0; i < COL_SIZE; i++) {
        for (int j = 0; j < ROW_SIZE; j++) {
            int t = qrand() % BLOCK_TYPE_NUM;
            mat[i][j] = t;
            eliminated[i][j] = false;
            DragPushButton* p = btns[i][j];
            p->setGeometry(
                GAME_FIELD_X + BLOCK_EDGE * j + BLOCK_MARGIN,
                GAME_FIELD_Y + BLOCK_EDGE * i + BLOCK_MARGIN,
                BLOCK_EDGE - BLOCK_MARGIN * 2,
                BLOCK_EDGE - BLOCK_MARGIN * 2);
            QPalette palette = p->palette();
            palette.setColor(QPalette::Button, b_color[t]);
            p->setPalette(palette);
        }
    }

    this->flush(false);
}

void MainWindow::set_score(int s)
{
    this->score = s;
    ui->lcdNumber->display(score);
}

void MainWindow::flush(bool animate)
{
    // 主检测循环，只要还能自动消除，
    // 就扫描一下然后消除并随机补充新的块，
    // 直到不能自动消除为止
    while (1) {
        //        for (int i = 0; i < COL_SIZE; i++)
        //            qDebug() << mat[i][0] << " "
        //                     << mat[i][1] << " "
        //                     << mat[i][2] << " "
        //                     << mat[i][3] << " "
        //                     << mat[i][4] << " "
        //                     << mat[i][5] << " "
        //                     << mat[i][6] << " "
        //                     << mat[i][7] << " "
        //                     << mat[i][8] << " "
        //                     << mat[i][9] << " "
        //                     << mat[i][10];
        match_scan();
        //        for (int i = 0; i < COL_SIZE; i++)
        //            qDebug() << eliminated[i][0] << " "
        //                     << eliminated[i][1] << " "
        //                     << eliminated[i][2] << " "
        //                     << eliminated[i][3] << " "
        //                     << eliminated[i][4] << " "
        //                     << eliminated[i][5] << " "
        //                     << eliminated[i][6] << " "
        //                     << eliminated[i][7] << " "
        //                     << eliminated[i][8] << " "
        //                     << eliminated[i][9] << " "
        //                     << eliminated[i][10];
        if (cal_score() == 0)
            break;
        drop(animate);
    }
}
